package com.ccit.greenlife.Mapper;

import com.ccit.greenlife.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

@Mapper//@Mapper 注解用于标记 Mapper 接口，允许 MyBatis 自动生成实现类，而不需要手动编写 XML 映射文件或使用注解来指定 SQL 语句(将接口直接注册到ioc容器中使用)
public interface UserMapper {
    @Select("select * from user where id=#{id}")
    User getUserInfo(String id);

    //根据用户名查询用户
    @Select("select * from user where username=#{username}")
    User findByUsername(String username);

    //根据邮箱查询用户
    @Select("select * from user where email=#{username}")
    User findByEmail(String username);

    //注册用户(已填充信息)
    //添加用户
    @Insert("insert into user(id, nickname, username, password, email, level, isActive, type, exp, createTime, integral) " +
            "values(#{id}, #{nickname}, #{username}, #{password}, #{email}, #{level}, #{isActive}, #{type}, #{exp}, #{createTime}, #{integral})")
    void add(User user);

    //更新密码
    @Update("update user set password=#{newPwd}, endTime=now() where id=#{id}")
    void updatePwd(String id, String newPwd);

    //更新头像
    @Update("update user set avatar=#{avatar} where id=#{id}")
    void updateAvatar(String avatar, String id);

    //更新用户信息
    @Update("update user set username=#{username}, endTime=now() where id=#{id}")
    void updateInfo(User user);
}
